package com.ysd.dao.Impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.Company;
import com.ysd.entity.Research;
import com.ysd.util.DBManager;

public class CompanyDaoImpl {//实现类 ，起名：实体类名DaoImpl
	//查询公司的所有内容
    public Company getCompany() {
    	Connection conn=DBManager.getConnection();//创建连接
    
    	Statement st=null;//控制作用域范围
    	ResultSet rs=null;
    	Company company=null;
    	try {//正常
			st=conn.createStatement();//创建statement对象，这个对象的作用是查询sql语句
			rs=st.executeQuery("select * from company");//使用sql语句
			while(rs.next()) {//遍历结果集
				int id=rs.getInt("id");
				String name=rs.getString("name");
				String webSite=rs.getString("webSite");
				String introduction=rs.getString("introduction");
				String phone=rs.getString("phone");
				String email=rs.getString("email");
				String address=rs.getString("address");
				String culture=rs.getString("culture");
				String logo=rs.getString("logo");
				company=new Company(name,webSite,introduction,phone,email,address,culture,logo,id);
			}
			return company;
		} catch (SQLException e) {//异常
			e.printStackTrace();
			return null;//返回空
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
    }
    
    //通过id获取公司内容
     public Company getCompanyById(int cId) {
    	 Company company=null;
    	 Connection conn=DBManager.getConnection();
         Statement st=null;
         ResultSet rs=null;
         try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM company WHERE id="+cId);
			while(rs.next()) {
				int id=rs.getInt("id");
				String name=rs.getString("name");
				String webSite=rs.getString("webSite");
				String introduction=rs.getString("introduction");
				String phone=rs.getString("phone");
				String email=rs.getString("email");
				String address=rs.getString("address");
				String culture=rs.getString("culture");
				String logo=rs.getString("logo");
				company=new Company(name,webSite,introduction,phone,email,address,culture,logo,id);
			}
			return company;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
     }
     /**
      * 修改公司内容  
      */
     public int updateCompanyById(Company c) {
    	 return DBManager.executeUpdate("UPDATE company SET name='"+c.getName()+"',webSite='"+c.getWebSite()+"',introduction='"+c.getIntroduction()+"',address='"+c.getAddress()+"',phone='"+c.getPhone()+"',email='"+c.getEmail()+"',culture='"+c.getCulture()+"',logo='"+c.getLogo()+"' WHERE id="+c.getId()+"");
     }
}
